<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>优田儿童成长中心</title>
    <link href="/static/assets/fontAwesome/css/fontawesome-all.min.css" rel="stylesheet">
    <link href="/static/assets/css/lib/themify-icons.css" rel="stylesheet">
    <link href="/static/assets/css/lib/mmc-chat.css" rel="stylesheet"/>
    <link href="/static/assets/css/lib/sidebar.css" rel="stylesheet">
    <link href="/static/assets/css/lib/bootstrap.min.css" rel="stylesheet">
    <link href="/static/assets/css/lib/nixon.css" rel="stylesheet">
    <link href="/static/assets/lib/lobipanel/css/lobipanel.min.css" rel="stylesheet">
    <link href="/static/assets/css/style.css" rel="stylesheet">
</head>

<body>
<div class="sidebar sidebar-hide-to-small sidebar-shrink sidebar-gestures">
    <div class="nano">
        <div class="nano-content">
            <ul>
                <li><a href="/home/"><i class="ti-bar-chart"></i> 统计数据</a></li>
                <li><a href="/teacher/"><i class="ti-user"></i> 教师管理</a></li>
                <li><a href="/child/"><i class="ti-user"></i> 幼儿管理</a></li>
                <li class="active"><a href="/posts/"><i class="ti-desktop"></i> 岗位管理<span
                        class="sidebar-collapse-icon ti-angle-right"></span></a></li>
            </ul>
        </div>
    </div>
</div>

<div class="header">
    <div class="pull-left">
        <div class="logo">
            <a><img id="logoImg" src="/static/img/yt1.png" data-logo_big="/static/img/yt1.png"
                    data-logo_small="/static/img/yt3.png" alt="Nixon"/></a>
        </div>
        <div class="hamburger sidebar-toggle"><span class="ti-menu"></span></div>
    </div>
    <div class="pull-right p-r-15">
        <ul>
            <li class="header-icon dib">
                <img class="avatar-img" src="/static/assets/images/avatar/1.jpg" alt=""/>
                <span class="user-avatar"><span th:text="${session.username}"></span>&nbsp;&nbsp;&nbsp;<i
                        class="ti-angle-down f-s-10"></i></span>
                <div class="drop-down dropdown-profile">
                    <div class="dropdown-content-body">
                        <ul>
                            <li><a href="/exit/"><i class="ti-power-off"></i> 注销</a></li>
                        </ul>
                    </div>
                </div>
            </li>
        </ul>
    </div>
</div>

<div class="content-wrap">
    <div class="main">
        <div class="container-fluid">
            <div class="row">
                <div class="col-lg-8 p-0">
                    <div class="page-header">
                        <div class="page-title"><h1>岗位管理</h1></div>
                    </div>
                </div>
                <div class="col-lg-4 p-0">
                    <div class="page-header">
                        <div class="page-title">
                            <ol class="breadcrumb text-right">
                                <li><a href="/posts/">岗位信息</a></li>
                                <li class="active">岗位管理</li>
                            </ol>
                        </div>
                    </div>
                </div>
            </div>

            <div class="main-content">
                <div class="row">
                    <div class="card alert">
                        <div class="card-header">
                            <h4>岗位信息</h4>
                            <div class="card-header-right-icon">
                                <a href="/add_posts/" class="btn btn-success m-b-10 m-l-5">添加岗位</a>
                            </div>
                        </div>
                        <div class="card-body">
                            <table class="table table-responsive table-hover">
                                <thead>
                                <tr>
                                    <th>编号</th>
                                    <th>岗位名称</th>
                                    <th>岗位工资</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody id="posts-table-body"></tbody>
                            </table>
                            <nav>
                                <ul class="pagination" id="pagination"></ul>
                            </nav>
                        </div>
                    </div>
                </div>
            </div>
            Copyright &copy; 2024
        </div>
    </div>
</div>

<script src="/static/assets/js/lib/jquery.min.js"></script>
<script src="/static/assets/js/lib/jquery.nanoscroller.min.js"></script>
<script src="/static/assets/js/lib/sidebar.js"></script>
<script src="/static/assets/js/lib/bootstrap.min.js"></script>
<script src="/static/assets/js/lib/mmc-common.js"></script>
<script src="/static/assets/js/lib/mmc-chat.js"></script>
<script src="/static/assets/js/lib/chart-js/Chart.bundle.js"></script>
<script src="/static/assets/js/lib/chart-js/chartjs-init.js"></script>
<script src="/static/assets/js/lib/datamap/d3.min.js"></script>
<script src="/static/assets/js/lib/datamap/topojson.js"></script>
<script src="/static/assets/js/lib/datamap/datamaps.world.min.js"></script>
<script src="/static/assets/js/lib/datamap/datamap-init.js"></script>
<script src="/static/assets/lib/lobipanel/js/lobipanel.js"></script>
<script src="/static/assets/js/scripts.js"></script>

<script>
    let currentPage = 1; // 保存当前页

    $(document).ready(function () {
        loadPostData(currentPage, 8);
    });

    function loadPostData(page = 1, size = 8) {
        currentPage = page;
        $.ajax({
            url: "/api/v1/manager/posts/list",
            method: "GET",
            data: {page: page, size: size},
            success: function (res) {
                if (res.code === 200) {
                    const pageData = res.data;
                    renderTable(pageData.list);
                    renderPagination(pageData.total, pageData.pageNum, pageData.pageSize);
                } else {
                    alert("加载失败：" + res.msg);
                }
            },
            error: function () {
                alert("请求失败，请检查网络");
            }
        });
    }

    function renderTable(list) {
        const tbody = $("#posts-table-body");
        tbody.empty();
        list.forEach(t => {
            const row = `
                    <tr>
                        <th scope="row">${t.pid}</th>
                        <td>${t.pname}</td>
                        <td>${t.post_sal}</td>
                        <td>
                            <a href="/update_posts/?pid=${t.pid}" class="btn btn-success m-l-5">编辑</a>
                            <a href="javascript:;" onclick="del(${t.pid})" class="btn btn-danger m-l-5">删除</a>
                        </td>
                    </tr>`;
            tbody.append(row);
        });
    }

    function renderPagination(total, currentPage, pageSize) {
        const totalPages = Math.ceil(total / pageSize);
        const pagination = $("#pagination");
        pagination.empty();

        // 上一页
        pagination.append(`<li class="page-item ${currentPage === 1 ? 'disabled' : ''}">
                <a class="page-link" href="javascript:;" data-page="${currentPage - 1}">上一页</a></li>`);

        // 页码
        const start = Math.max(1, currentPage - 2);
        const end = Math.min(totalPages, currentPage + 2);
        if (start > 1) pagination.append(`<li class="page-item"><a class="page-link" data-page="1">1</a></li>`);
        if (start > 2) pagination.append(`<li class="page-item disabled"><a class="page-link">...</a></li>`);

        for (let i = start; i <= end; i++) {
            pagination.append(`<li class="page-item ${i === currentPage ? 'active' : ''}">
                    <a class="page-link" href="javascript:;" data-page="${i}">${i}</a></li>`);
        }

        if (end < totalPages - 1) pagination.append(`<li class="page-item disabled"><a class="page-link">...</a></li>`);
        if (end < totalPages) pagination.append(`<li class="page-item"><a class="page-link" data-page="${totalPages}">${totalPages}</a></li>`);

        // 下一页
        pagination.append(`<li class="page-item ${currentPage === totalPages ? 'disabled' : ''}">
                <a class="page-link" href="javascript:;" data-page="${currentPage + 1}">下一页</a></li>`);

        $(".pagination .page-link").off("click").on("click", function () {
            const $parent = $(this).parent();
            if ($parent.hasClass("disabled") || $parent.hasClass("active")) {
                return; // 禁止点击 disabled 或 active 的页码
            }

            const page = parseInt($(this).data("page"));
            if (!isNaN(page)) {
                loadPostData(page);
            }
        });
    }

    function del(pid) {
        if (confirm("确认删除该岗位？")) {
            $.ajax({
                url: "/api/v1/manager/posts/delete/" + pid,  // 注意URL和后端保持一致
                type: "DELETE",
                success: function(res) {
                    if (res.code === 200) {
                        alert("删除成功！");
                        loadPostData(currentPage); // 删除后刷新当前页数据
                    } else {
                        alert("删除失败：" + res.msg);
                    }
                },
                error: function() {
                    alert("请求失败，请检查网络");
                }
            });
        }
    }
</script>
</body>
</html>
